package com.burgeon.backend.EnterpriseWeChat.service.Impl;

import com.burgeon.backend.EnterpriseWeChat.entity.*;
import com.burgeon.backend.EnterpriseWeChat.mappers.EWVipWardobeMapper;
import com.burgeon.backend.EnterpriseWeChat.rest.VipClothespress.getVipWardrobe;
import com.burgeon.backend.EnterpriseWeChat.rest.VipClothespress.getVipWardrobeStoreAndDocno;
import com.burgeon.backend.EnterpriseWeChat.rest.VipInfo.*;
import com.burgeon.backend.EnterpriseWeChat.rest.action.Delete;
import com.burgeon.backend.EnterpriseWeChat.service.EWVipService;
import com.burgeon.backend.EnterpriseWeChat.service.EWVipWardrobeService;
import com.burgeon.backend.EnterpriseWeChat.service.ex.UpDataException;
import com.burgeon.backend.EnterpriseWeChat.utils.TestUrlUtils;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class EWVipWardrobeServiceImpl implements EWVipWardrobeService {
    private final static Log logger = LogFactory.getLog(EWVipService.class);
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private EWVipWardobeMapper ewVipWardobeMapper;

    @Override
    public ArrayList<WardrobeData> getVipWardrobe(String token, String vipCardNo) {
        Info info = GetInfoByToken(token);
        getVipWardrobeStoreAndDocno getVipWardrobeStoreAndDocno = new getVipWardrobeStoreAndDocno(info.getUrl(),
                info.getAccount(),info.getPassword());
        getVipWardrobe getVipWardrobe = new getVipWardrobe(info.getUrl(),info.getAccount(),info.getPassword());
        ArrayList<Wardrobe> wardrobes = new ArrayList<>();
        ArrayList<WardrobeData> wardrobeDatas= null;
        try {
            //店仓
            wardrobeDatas = getVipWardrobeStoreAndDocno.getData(vipCardNo);
            //去重
            wardrobeDatas = removeDuplicate_3(wardrobeDatas);
            //商品
            wardrobes = getVipWardrobe.getData(vipCardNo,token);
            for (WardrobeData wardrobeData:wardrobeDatas) {
                ArrayList<Wardrobe> wardrobes1 = new ArrayList<>();
                for (Wardrobe wardrobe:wardrobes) {
                    if (wardrobe.getStoreName().equals(wardrobeData.getStoreName())){
                        String jpgUrl = "http://"+info.getUrl() + "/html/M_PRODUCT/" + wardrobe.getProductName()+".jpg";
                        String pngUrl = "http://"+info.getUrl() + "/html/M_PRODUCT/" + wardrobe.getProductName()+".png";
                        if(TestUrlUtils.testUrl(jpgUrl)){
                            System.err.println(jpgUrl);
                            wardrobe.setProductImg(jpgUrl);
                        }else if (TestUrlUtils.testUrl(pngUrl)){
                            System.err.println(pngUrl);
                            wardrobe.setProductImg(pngUrl);
                        }else {
                            wardrobe.setProductImg("http://lfwd.cloudsmicro.com:8080/img/EnterpriseWeChat/product.png");
                        }
                        wardrobes1.add(wardrobe);
                    }
                }
                wardrobeData.setWardrobes(wardrobes1);
            }

        } catch (Exception e) {
            e.printStackTrace();
            logger.info("衣橱"+e.getMessage());
            throw new UpDataException("衣橱"+e.getMessage());
        }
        return wardrobeDatas;
    }

    @Override
    public VipContact getVipContactInfo(String token, String vipId) {
        Info info = GetInfoByToken(token);
        getVipContactInfo getVipContactInfo = new getVipContactInfo(info.getUrl(),info.getAccount(),info.getPassword());
        queryVipDesc queryVipDesc = new queryVipDesc(info.getUrl(),info.getAccount(),info.getPassword());
//        VipContact vipContact = new VipContact();
        try {
            VipContact vipContact = getVipContactInfo.getData(vipId);
            if(vipContact != null){
                String desc = queryVipDesc.getData(vipId);
                if(desc!=null&&!desc.equals("null")){
                    vipContact.setDesc(desc);
                }
                return vipContact;
            }


        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException(e.getMessage());
        }
        return null;
    }

    @Override
    public void saveUserBasicInfo(String token, String vipId, VipContact vipContact) {
        Info info = GetInfoByToken(token);
        saveUserBasicInfo saveUserBasicInfo = new saveUserBasicInfo(info.getUrl(),info.getAccount(),info.getPassword());
        saveVipDes saveVipDes = new saveVipDes(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            saveUserBasicInfo.getData(vipId, vipContact);
            if (!vipContact.getDesc().equals("null")){
                saveVipDes.getData(vipId,vipContact.getDesc());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException(e.getMessage());
        }
    }

    @Override
    public  ArrayList<Referral> getReferralList(String token, String vipId) {
        Info info = GetInfoByToken(token);
        getReferralList getReferralList = new getReferralList(info.getUrl(),info.getAccount(),info.getPassword());
        ArrayList<Referral> referral = new ArrayList<>();
        try {
            referral = getReferralList.getData(vipId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException(e.getMessage());
        }
        return referral;
    }

    @Override
    public void deleteReferralList(String token, String id) {
        Info info = GetInfoByToken(token);
        Delete delete = new Delete(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            delete.getData("INTRODUCING_CUSTOMERS",id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException(e.getMessage());
        }
    }

    @Override
    public String createReferral(String token, String cardNo, Referral referral) {
        Info info = GetInfoByToken(token);
        createReferral createReferral = new createReferral(info.getUrl(),info.getAccount(),info.getPassword());
        String obj = null;
        try {
            obj = createReferral.getData(cardNo, referral);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException(e.getMessage());
        }
        return obj;
    }

    @Override
    public void modifyReferral(String token, Referral referral) {
        Info info = GetInfoByToken(token);
        modifyReferral modifyReferral = new modifyReferral(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            modifyReferral.getData(referral);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException(e.getMessage());
        }
    }

    @Override
    public HashMap getVipFearture(String token, String vipCardNo) {
        Info info = GetInfoByToken(token);
        HashMap map = new HashMap();
        getVipFearture getVipFearture = new getVipFearture(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            map = getVipFearture.getData(vipCardNo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException(e.getMessage());
        }
        return map;
    }

    /**
     * 推荐的用户信息
     * @param phone
     * @param token
     * @return
     */
    @Override
    public List<ReVip> getDataByRid(String phone, String token) {
        //通过顾客手机号
        List<String> ids = ewVipWardobeMapper.getDataIds(phone,token);
        if(ids.size()!=0){
            List<ReVip> vips = new ArrayList<>();
            for (String id:ids) {
                ReVip vip = ewVipWardobeMapper.getDataByRid(id,token);
                if(vip!=null) {
                    vips.add(vip);
                }
            }
            return vips;
        }else {
            return null;
        }
    }

    //3.利用list的contains方法去重
    public static ArrayList<WardrobeData>  removeDuplicate_3(ArrayList<WardrobeData>  list){
//        ArrayList<WardrobeData>  tempList = new ArrayList<WardrobeData> (list.size());
        for(int i =0;i<list.size()-1;i++){
            for(int j=list.size()-1;j>i;j--){
                if(list.get(i).getStoreName().equals(list.get(j).getStoreName()))
                    list.remove(j);
            }
        }
        return list;
    }
    private Info GetInfoByToken(String token) {
        return accountMapper.GetTokenData(token);
    }

}
